package hero.mps.web.school_major.contoller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hero.mps.utils.ResultUtils;
import hero.mps.utils.ResultVo;
import hero.mps.web.school_college.entity.SchoolCollege;
import hero.mps.web.school_college.service.SchoolCollegeService;
import hero.mps.web.school_major.entity.MajorParam;
import hero.mps.web.school_major.entity.SchoolMajor;
import hero.mps.web.school_major.service.SchoolMajorService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/major")
@RequiredArgsConstructor
public class SchoolMajorController {
    private final SchoolMajorService schoolMajorService;
    private final SchoolCollegeService schoolCollegeService;

    /**
     * 新增专业
     * @param schoolMajor
     * @return
     */
    @PostMapping
    public ResultVo add(@RequestBody SchoolMajor schoolMajor) {
        boolean save = schoolMajorService.save(schoolMajor);
        if (save) {
            return ResultUtils.success("新增专业成功");
        }else {
            return  ResultUtils.error("新增专业失败");
        }
    }

    /**
     * 编辑专业
     * @param schoolMajor
     * @return
     */
    @PutMapping
    public ResultVo edit(@RequestBody SchoolMajor schoolMajor) {
        boolean update = schoolMajorService.updateById(schoolMajor);
        if (update) {
            return ResultUtils.success("编辑专业成功");
        }else {
            return  ResultUtils.error("编辑专业失败");
        }
    }

    /**
     * 删除专业
     * @param majorId
     * @return
     */
    @DeleteMapping("/{majorId}")
    public ResultVo delete(@PathVariable("majorId") Integer majorId) {
        boolean remove = schoolMajorService.removeById(majorId);
        if (remove) {
            return ResultUtils.success("删除专业成功");
        }else {
            return  ResultUtils.error("删除专业失败");
        }
    }

    /**
     * 查询专业列表
     * @param majorParam
     * @return
     */
    @GetMapping("/list")
    public ResultVo list(MajorParam majorParam) {
        IPage<SchoolMajor> list = schoolMajorService.getList(majorParam);
        return ResultUtils.success("查询成功", list);
    }

    /**
     * 查询学院列表
     * @param
     * @return
     */
    @GetMapping("/college")
    public ResultVo getCollegeList() {
        List<SchoolCollege> list = schoolCollegeService.list();
        return ResultUtils.success("查询成功", list);
    }

}
